package com.fattyCorps.controller;


import com.fattyCorps.obj.cache.ChatInfo;
import com.fattyCorps.obj.msg.SRet;
import com.fattyCorps.obj.msg.client.chat.CChatMsg;
import com.fattyCorps.obj.msg.client.chat.CGetChatListMsg;
import com.fattyCorps.obj.msg.server.chat.SChatMsg;
import com.fattyCorps.obj.msg.server.chat.SGetChatListMsg;
import com.fattyCorps.service.ChatService;
import com.fattyCorps.utils.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/chat")
public class ChatController {

    @Autowired
    ChatService chatService;

    @PostMapping("/getChatList")
    public SRet getChatList(HttpServletRequest request, @RequestBody CGetChatListMsg cGetChatListMsg){
        List<ChatInfo> list = chatService.getChatList(cGetChatListMsg.getChatId());
        SGetChatListMsg msg = new SGetChatListMsg();
        msg.setChatInfoList(list);
        return SRet.success(msg);
    }

    @PostMapping("/chat")
    public SRet chat(HttpServletRequest request, @RequestBody CChatMsg cChatMsg) {
        Integer playerId = ServletUtils.getPlayerIdByRequest(request);
        chatService.chat(playerId, cChatMsg.getContent());
        List<ChatInfo> list = chatService.getChatList(cChatMsg.getChatId());
        SChatMsg msg = new SChatMsg();
        msg.setChatInfoList(list);
        return SRet.success(msg);
    }

}
